package com.boco.modules.fdoc.web.sigserverpack;

import java.util.Date;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.boco.common.web.ServletController;
import com.boco.modules.fdoc.model.SigServicepackEntity;
import com.boco.modules.fdoc.model.SigSvsPackDtlRlnEntity;
import com.boco.modules.fdoc.service.SigServicepackService;
import com.boco.modules.fdoc.service.SigSvsPackDtlRlnService;
import com.boco.modules.fdoc.vo.SigServicepackVo;

@Controller
@RequestMapping(value = "/sigserverpack")
public class AddSigSvsPackCtr extends ServletController {

	@Resource
	SigServicepackService sigServicepackService;
	@Resource
	SigSvsPackDtlRlnService sigSvsPackDtlRlnService;

	/**
	 * 新增家庭信息
	 */
	@RequestMapping(value = "/addSvsPack",method = RequestMethod.POST)
	public String addSvsPack(SigServicepackVo vo, HttpServletRequest request, HttpServletResponse response,
			Model model) {
		System.out.println(sigServicepackService.isExistSigSvsPack(vo.getPackName()));
		if (sigServicepackService.isExistSigSvsPack(vo.getPackName())) {
			request.setAttribute("nameagain", "新增签约包名称重复！");
			return "/sigsvspack/stsAddSvsPack";
		}else {

			SigServicepackEntity sigServicepackEntity = new SigServicepackEntity();
			BeanUtils.copyProperties(vo, sigServicepackEntity);
			// 将数据库类型为int、decimal的字段重新赋值
			String packName=vo.getPackName();
			sigServicepackEntity.setPackName(packName);
			sigServicepackEntity.setPackPrice(vo.getPackPrice());
			sigServicepackEntity.setUserPay(vo.getUserPay());
			sigServicepackEntity.setOrgratio(vo.getOrgratio());
			sigServicepackEntity.setAdviceGroup(vo.getAdviceGroup());
			sigServicepackEntity.setTarget(vo.getTarget());
			Date nowTime = new Date();
			String nowUser = "ken";
			sigServicepackEntity.setCreateBy(nowUser);
			sigServicepackEntity.setCreateTime(nowTime);
			sigServicepackEntity.setUpdateBy(nowUser);
			sigServicepackEntity.setUpdateTime(nowTime);
			sigServicepackEntity.setRemarks(vo.getRemarks());
			sigServicepackEntity.setDelFlag("0");
			sigServicepackService.insertSvsPackInfo(sigServicepackEntity);
			
			SigServicepackVo servicepackVo=sigServicepackService.getSigSvsPackByName(packName);
			int sigSvsPackId=servicepackVo.getId();
			System.out.println("===================================="+sigSvsPackId+"================");
			request.setAttribute("sigSvsPackId",sigSvsPackId);
			int[] svsPackDtlIds=vo.getSvsPackDtlIds();
			if (svsPackDtlIds!=null&&svsPackDtlIds.length>0) {
				SigSvsPackDtlRlnEntity sigSvsPackDtlRlnEntity=null;
				for(int svsPackDtlId:svsPackDtlIds){
					System.out.println(svsPackDtlId+"===================服务详情列表");
					sigSvsPackDtlRlnEntity=new SigSvsPackDtlRlnEntity();
					sigSvsPackDtlRlnEntity.setSigId(sigSvsPackId);
					sigSvsPackDtlRlnEntity.setDelFlag("0");
					sigSvsPackDtlRlnEntity.setDetailsId(svsPackDtlId);
					sigSvsPackDtlRlnService.insertSvsPackDtlInfo(sigSvsPackDtlRlnEntity);
				}
				request.setAttribute("sucessAdd", "新增签约服务包及相关服务详情记录成功！");
				return "/sigsvspack/stsAddSvsPack";
			}else {
				request.setAttribute("svspacknull", "服务详情包为空！");
				return "/sigsvspack/stsAddSvsPack";
			}
			
		}
		
	}
	}


